<?php
namespace Home\Controller;

use Think\Controller;

class XiangceController extends VController
{

    public function index()
    {
        $uid = $this->userData['id'];
        $AModel = M('Album');
        $albumData = $AModel->field('qzone_album.id,qzone_album.album_name,qzone_album_photo.path,qzone_album.album_describe,qzone_album.ctime')->join('qzone_album_photo ON qzone_album.id=qzone_album_photo.album_id AND qzone_album_photo.cover=1', 'LEFT')->where("qzone_album.uid='{$uid}'")->select();
        $this->assign('myUserData', $this->myUserData);
        $this->assign('userData', $this->userData);
        $this->assign('isMy', $this->isMy);
        $this->assign('albumData', $albumData);
        $this->display();

    }
    public function photo()
    {
        $album_id = I('get.album_id');
        $Album = D("Album");
        $albumData = $Album->relation(true)->where("id={$album_id}")->find();
        $this->assign('myUserData', $this->myUserData);
        $this->assign('userData', $this->userData);
        $this->assign('isMy', $this->isMy);
        $this->assign('albumData', $albumData);
        $this->assign('album_id', $album_id);
        $this->display();
    }

    public function shangchuan()
    {
        $uid = 4;
        $album_photo = M('album_photo');
        $aid = I('get.aid');
        $album_photoData = $album_photo->table('qzone_album qa,qzone_album_photo qap')->field('qa.id,qa.uid,qap.path,qap.id,qap.album_id')->where("qa.id=qap.album_id AND qa.id={$aid}")->select();
        $this->assign('album_photoData', $album_photoData);
        $this->assign('aid', I('get.aid'));
        $this->display('xiangce/shangchuan');
    }

    public function createAlbum()
    {
        if (IS_AJAX) {
            $data = I('post.');
            $data['uid'] = $this->myUserData['id'];
            $data['ctime'] = time();
            $album = M('album');
            $album->create($data);
            $result = $album->add($data);
            if ($result > 0) {
                $this->ajaxReturn(array('status' => 0, 'message' => '创建成功！', 'ctime' => date('Y-m-d H:i',$album->ctime), 'id' => $result));
            }
            $this->ajaxReturn(array('status' => 1, 'message' => '创建失败！'));

        } else {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法来源！'));
        }
    }

    public function editAlbum()
    {
        if (IS_AJAX) {
            //判断是否为相册主人
            $model = M('Album');
            $MData = $model->find(I('post.id'));
            if ($MData['uid'] != $this->myUserData['id']) {
                $this->ajaxReturn(array('status' => 1, 'message' => '您不是相册的主人，无法编辑！'));
            }
            $model->create();
            $result = $model->save();
            if ($result > 0) {
                $this->ajaxReturn(array('status' => 0, 'message' => '编辑成功！'));
            }
            $this->ajaxReturn(array('status' => 1, 'message' => '编辑失败！'));
        } else {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法来源！'));
        }
    }
    public function editPhoto()
    {
        if (IS_AJAX) {
            $model = M('AlbumPhoto');
            //判断是否为照片主人
            if ($model->field('uid')->find(I('post.id'))['uid'] != $this->myUserData['id']) {
                $this->ajaxReturn(array('status' => 1, 'message' => '您不是相册的主人，无法编辑！'));
            }

            $model->create();
            $result = $model->save();
            if ($result > 0) {
                $this->ajaxReturn(array('status' => 0, 'message' => '编辑成功！'));
            }
            $this->ajaxReturn(array('status' => 1, 'message' => '编辑失败！'));
        } else {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法来源！'));
        }
    }

//设置封面
    public function szfm()
    {
        $aid = I('POST.album_id');
        $id = I('POST.id');

        $model = M('album_photo');
        $data['cover'] = '0';
        $res = $model->where("album_id=$aid")->save($data);
        $data['cover'] = '1';
        $res = $model->where("id=$id")->save($data);
        if ($res > 0) {
            $this->ajaxReturn(array('status' => '0'));
        } else {
            $this->ajaxReturn(array('status' => '1'));
        }
    }

//删除照片
  
  public function sczp(){
     
        $id = I('POST.id');
        $path=I('POST.path');
        $model = M('album_photo');
        
       

       $result = unlink('./Public'.$path);       
       
   $res = $model->delete($id);
     
        if($res>0){
            $this->ajaxReturn(array('status'=>'0'));
               
        } else {
            $this->ajaxReturn(array('status'=>'1'));
        }

        $id = I('POST.id');
        $path = I('POST.path');
        $model = M('album_photo');
        $result = unlink('./Public' . $path);
        $res = $model->delete($id);
        if ($res > 0) {
            $this->ajaxReturn(array('status' => '0'));

        } else {
            $this->ajaxReturn(array('status' => '1'));
        }
    }

//删除相册
    public function delAlbum()
    {
        if (IS_AJAX) {
            $id = I('POST.id');
            $model = M('album');
            if ($model->field('uid')->find($id)['uid'] != $this->myUserData['id']) {
                $this->ajaxReturn(array('status' => 1, 'message' => '您不是相册的主人，无法删除！'));
            }
            $res = $model->delete($id);
            if ($res > 0) {
                $this->ajaxReturn(array('status' => '0','message' => '删除成功！'));
            } else {
                $this->ajaxReturn(array('status' => '1','message' => '删除失败！'));
            }
        } else {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法来源！'));
        }
    }

    //删除照片
    public function delPhoto()
    {
        if (IS_AJAX) {
            $id = I('POST.id');
            $model = M('AlbumPhoto');
            if ($model->field('uid')->find($id)['uid'] != $this->myUserData['id']) {
                $this->ajaxReturn(array('status' => 1, 'message' => '您不是照片的主人，无法删除！'));
            }
            $res = $model->delete($id);
            if ($res > 0) {
                $this->ajaxReturn(array('status' => '0','message' => '删除成功！'));
            } else {
                $this->ajaxReturn(array('status' => '1','message' => '删除失败！'));
            }
        } else {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法来源！'));
        }
    }

    public function xiangceUpload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = '../Public/uploads/'; // 设置附件上传根目录
        $upload->subName = array('date', 'Ymd'); // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息

            $this->ajaxReturn(array('status' => 1));
        } else {// 上传成功
            $path = substr($info['Filedata']['savepath'], 9) . $info['Filedata']['savename'];
            $id = I('get.album_id');
            //将图片插入数据库
            $user = M('album_photo');
            $data['uid'] = 4;
            $data['album_id'] = $id;
            $data['path'] = $path;
            $data['cover'] = 0;
            $photo_id = $user->add($data);

            $this->ajaxReturn(array('status' => 0, 'path' => $path, 'id' => $photo_id));
        }
    }
}
